package com.openspring.platform.service.impl;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.openspring.framework.service.AbstractGenericService;
import com.openspring.platform.dao.DictionaryDAO;
import com.openspring.platform.dao.DictDAO;
import com.openspring.platform.model.Dictionary;
import com.openspring.platform.model.Dict;
import com.openspring.platform.service.DictionaryService;

@Service("dictionaryService")
public class DictionaryServiceImpl extends AbstractGenericService<Dictionary, Long> implements DictionaryService {

	private DictionaryDAO dictionaryDAO;
	private DictDAO dictDAO;

	@Resource(name="dictionaryDAO")
	public void setDictionaryDAO(DictionaryDAO dictionaryDAO) {
		this.dictionaryDAO = dictionaryDAO;
		super.setGenericDAO(dictionaryDAO);
	}

	@Resource(name="dictDAO")
	public void setDictDAO(DictDAO dictDAO) {
		this.dictDAO = dictDAO;
	}

	@Override
	public Map<String, Map<String, Dictionary>> findDictionarys() {
		Map<String, Map<String, Dictionary>> map = new LinkedHashMap<String, Map<String, Dictionary>>();
		for(Dict dict : dictDAO.queryAll()) {
			Map<String, Dictionary> map1 = new LinkedHashMap<String, Dictionary>();
			for(Dictionary dictionary : dictionaryDAO.queryWhere(" WHERE \"dictId\" = ? ORDER BY sort", dict.getId())) {
	    		map1.put(dictionary.getValue(), dictionary);
	    	}
			map.put(dict.getCode(), map1);
		}
		return map;
	}

	@Override
	public List<Dictionary> findByDictId(Long dictId) {
		return dictionaryDAO.queryByDictId(dictId);
	}

	@Override
	public List<Dictionary> findByDictCode(String dictCode) {
		return dictionaryDAO.queryByDictCode(dictCode);
	}
}
